package org.dromara.Nyt7InsuranceCategory.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7InsuranceCategory.domain.bo.Nyt7InsuranceCategoryBo;
import org.dromara.Nyt7InsuranceCategory.domain.vo.Nyt7InsuranceCategoryVo;
import org.dromara.Nyt7InsuranceCategory.domain.Nyt7InsuranceCategory;
import org.dromara.Nyt7InsuranceCategory.mapper.Nyt7InsuranceCategoryMapper;
import org.dromara.Nyt7InsuranceCategory.service.INyt7InsuranceCategoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 产品分类Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-26
 */
@RequiredArgsConstructor
@Service
public class Nyt7InsuranceCategoryServiceImpl implements INyt7InsuranceCategoryService {

    private final Nyt7InsuranceCategoryMapper baseMapper;

    /**
     * 查询产品分类
     *
     * @param categoryId 主键
     * @return 产品分类
     */
    @Override
    public Nyt7InsuranceCategoryVo queryById(Long categoryId){
        return baseMapper.selectVoById(categoryId);
    }


    /**
     * 查询符合条件的产品分类列表
     *
     * @param bo 查询条件
     * @return 产品分类列表
     */
    @Override
    public List<Nyt7InsuranceCategoryVo> queryList(Nyt7InsuranceCategoryBo bo) {
        LambdaQueryWrapper<Nyt7InsuranceCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Nyt7InsuranceCategory> buildQueryWrapper(Nyt7InsuranceCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7InsuranceCategory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), Nyt7InsuranceCategory::getCategoryName, bo.getCategoryName());
        lqw.eq(bo.getParentId() != null, Nyt7InsuranceCategory::getParentId, bo.getParentId());
        return lqw;
    }

    /**
     * 新增产品分类
     *
     * @param bo 产品分类
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7InsuranceCategoryBo bo) {
        Nyt7InsuranceCategory add = MapstructUtils.convert(bo, Nyt7InsuranceCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改产品分类
     *
     * @param bo 产品分类
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7InsuranceCategoryBo bo) {
        Nyt7InsuranceCategory update = MapstructUtils.convert(bo, Nyt7InsuranceCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7InsuranceCategory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除产品分类信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
